<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Weixin_Wresponse_module extends CI_Module
{
    private $data;
    private $sid;
    private $site_shop_count;

    public function __construct()
    {
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->site_shop_count = !empty($GLOBALS['config']['site_shop_count']) ? $GLOBALS['config']['site_shop_count'] : 10;
        $this->load->model('admin.Upload_model');
        $this->load->model('weixin.Response_model');
       
    }

    /**
     * 响应消息列表
     *
     */

    public function response_text_list()
    {
        $condition['word'] = addslashes($this->input->get_post('word'));
        $where  = ''  ;
        if($condition['word']){
            $where.= " ( content LIKE '%".$condition['word']."%')";
        }
        $page_config['perpage']=20;   //每页条数
        $page_config['part']=2;//当前页前后链接数量
        $page_config['url']='weixin/wresponse/response_text_list';//url
        $page_config['param'] = '?word='.$condition['word'] ;//参数
        $page_config['seg']=4;//参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
        $page_config['nowindex']=$this->uri->segment($page_config['seg']) ? $this->uri->segment($page_config['seg']):1;//当前页
        $page_config['total'] = $this->Response_model->get_response_text_count($where);
        $this->load->library('mypageclass');
        $this->mypageclass->initialize($page_config);
        $this->data['response_array'] = $this->Response_model->get_response_text_list($where,$page_config['perpage'],$page_config['nowindex']);
        $this->data['form_url'] = site_url('weixin/wresponse/response_text_list');
        $this->data['condition'] = $condition;
        $this->load->view('/xenon/wresponse_text_list',$this->data);
    }

    /**
     * 删除响应消息
     * @param   POST 表单数据
     * return true/false
     */

    public function delete_response_text()
    {
        $id = $this->input->get('id');
        $res = $this->Response_model->_delete_response_text($id);
        if($res){
            $this->success('操作成功',site_url('weixin/wresponse/response_text_list'),true);
        }
        $this->error('操作失败','',true);
    }

    /**
     * 关键词列表
     *
     */

    public function keywords_list()
    {
        $condition['word'] = addslashes($this->input->get_post('word'));
        $where  = ''  ;
        if($condition['word']){
            $where.= "( keywords LIKE '%".$condition['word']."%')";
        }
        $page_config['perpage']=20;   //每页条数
        $page_config['part']=2;//当前页前后链接数量
        $page_config['url']='weixin/wresponse/keywords_list';//url
        $page_config['param'] = '?word='.$condition['word'] ;//参数
        $page_config['seg']=4;//参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
        $page_config['nowindex']=$this->uri->segment($page_config['seg']) ? $this->uri->segment($page_config['seg']):1;//当前页
        $page_config['total'] = $this->Response_model->get_keywords_count($where);
        $this->load->library('mypageclass');
        $this->mypageclass->initialize($page_config);
        $keywords_array = $this->Response_model->get_keywords_list($where,$page_config['perpage'],$page_config['nowindex']);
        if($keywords_array)
        {
            foreach($keywords_array as $key=>$val)
            {
                if($val['type']=='1')
                {
                    $keywords_array[$key]['preview_data'] = unserialize($val['content']);
                }
                if($val['type']=='2')
                {
                    $line_numbers = _process_string_to_array($val['content']);
                    $line_numbers = array_slice($line_numbers,0,10);
                    $keywords_array[$key]['preview_data']  = $this->Line_model->get_line_list($line_numbers);
                }
            }
        }
        $this->data['keywords_array'] = $keywords_array;
        $this->data['form_url'] = site_url('weixin/wresponse/keywords_list');
        $this->data['condition'] = $condition;
        $this->load->view('/xenon/wkeywords_list',$this->data);
    }

    /**
     * 添加关键词
     */
    public function add_keywords()
    {
        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $form_data = $this->input->post('Form');
            if($form_data['type']=='1')
            {
                $temp_array = array(

                    'title'=>$form_data['title'],
                    'description'=>$form_data['description'],
                    'pic'=>$form_data['pic'],
                    'url'=>$form_data['url']

                );
                $content = serialize($temp_array);
            }

            if($form_data['type']=='2')
            {
                $content = $form_data['content'];
            }
            $data_array = array(
                'sid'=>$this->sid ,
                'keywords'=>$form_data['keywords'] ,
                'content'=>$content ,
                'crts'=>date('Y-m-d H:i:s'),
                'type'=>$form_data['type']
            );

            $result_id = $this->Response_model->_add_keywords($data_array) ;
            if($result_id)
            {
                $this->success('操作成功',site_url('weixin/wresponse/keywords_list'),true);
            }
            $this->error('操作失败',site_url('weixin/wresponse/add_keywords'),true);
        }

        $this->data['form_url'] = site_url('weixin/wresponse/add_keywords');
        $this->load->view('/xenon/wkeywords',$this->data);
    }


    /**
     * 编辑关键词
     */
    public function edit_keywords()
    {

        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $form_data = $this->input->post('Form');
            $id = $this->input->get_post('id');
            if($form_data['type']=='1')
            {
                $temp_array = array(
                    'title'=>$form_data['title'],
                    'description'=>$form_data['description'],
                    'pic'=>$form_data['pic'],
                    'url'=>$form_data['url']
                );
                $content = serialize($temp_array);
            }

            if($form_data['type']=='2')
            {
                $content = $form_data['content'];
            }
            $data_array = array(
                'sid'=>$this->sid ,
                'type'=>$form_data['type'],
                'keywords'=>$form_data['keywords'] ,
                'content'=>$content ,
                'crts'=>date('Y-m-d H:i:s')
            );

            $result_id = $this->Response_model->_edit_keywords($data_array,array('id'=>$id));
            if($result_id)
            {
               $this->success('操作成功',site_url('weixin/wresponse/keywords_list'),true);
            }
            $this->error('操作失败',site_url('weixin/wresponse/edit_keywords'),true);
        }


        $id = $this->uri->segment(4);
        $row = $this->Response_model->get_row("id = $id");

        if($row['type']=='1')
        {
            $url_data = unserialize($row['content']);
            if(is_array($url_data)) $row = array_merge($url_data,$row);
        }
        $this->data = $row ;
        $this->data['id'] = $id ;
        $this->data['form_url'] = site_url('weixin/wresponse/edit_keywords');
        $this->load->view('/xenon/wkeywords',$this->data);
    }

    /**
     * 删除关键词
     * @param   POST 表单数据
     * return true/false
     */

    public function delete_keywords()
    {
        $id = $this->input->get('id');
        $res = $this->Response_model->_delete_keywords($id);
        if($res){
            $this->success('操作成功',site_url('weixin/wresponse/keywords_list'),true);
        }
        $this->error('操作失败','',true);
    }
}